#pragma once
#include "oj_model.hpp"
// #include"oj_model_mysql.hpp"

#include <ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;

    //渲染网页的路径
    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View(){};
        ~View(){};

        void AllExpandHtml(std::vector<Question> &allQuestions, std::string *html)
        {
            //题目编号 题目标题 题目难度
            //推荐使用表格显示
            
            // 1.形成路径
            std::string src_html = template_path + "allQuestions.html";
            
            // 2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : allQuestions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }
            // 3. 获取被渲染的网页
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4.开始渲染,渲染结果是html
            tpl->Expand(html, &root);
        }
        void OneExpandHtml(const Question &q, std::string *html)
        {
            std::string src_html = template_path + "oneQuestion.html";

            ctemplate::TemplateDictionary root("oneQuestion");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            tpl->Expand(html, &root);
        }
    };
}
